<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="赛事名称" prop="matchName">
        <el-input
          v-model="queryParams.matchName"
          placeholder="请输入赛事名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场馆名称" prop="venueName">
        <el-input
          v-model="queryParams.venueName"
          placeholder="请输入场馆名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场地名称" prop="fieldName">
        <el-input
          v-model="queryParams.fieldName"
          placeholder="请输入场地名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="比赛日期" prop="matchDate">
        <el-date-picker clearable
          v-model="queryParams.matchDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择比赛日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['match:match:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['match:match:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['match:match:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['match:match:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="matchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="赛事名称" align="center" prop="matchName" />
      <el-table-column label="场馆名称" align="center" prop="venueName" />
      <el-table-column label="场地名称" align="center" prop="fieldName" />
      <el-table-column label="赛事介绍" align="center" prop="matchIntroduce" />
      <el-table-column label="赛事logo" align="center" prop="matchLogo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.matchLogo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="比赛日期" align="center" prop="matchDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.matchDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="matchStartTime" />
      <el-table-column label="结束时间" align="center" prop="matchEndTime" />
      <el-table-column label="比赛最大人数" align="center" prop="matchMaxPeople" />
      <el-table-column label="预约人数" align="center" prop="matchAppointmentsPeople" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['match:match:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['match:match:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改赛事管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="场地场馆" >
<!--

<el-option v-for="(item) in singleList" :key="item.singleNo" :label="item.singleName"
                  :value="item.singleNo" @click.native="clickSingle(item.singleNo)">
                </el-option>
-->
          <el-select v-model="form.venueId" placeholder="请选择场馆" >
            <el-option
              v-for="item in venueList"
              :key="item.venueId"
              :label="item.venueName"
              :value="item.venueId"
              @click.native="clickVenue(item.venueId)"
            >
            </el-option>
          </el-select>
-
          <el-select v-model="form.fieldId" placeholder="请选择场地">
            <el-option
              v-for="item in fieldList"
              :key="item.fieldId"
              :label="item.fieldName"
              :value="item.fieldId">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="赛事名称" prop="matchName">
          <el-input v-model="form.matchName" placeholder="请输入赛事名称" />
        </el-form-item>
        <el-form-item label="赛事介绍" prop="matchIntroduce">
          <el-input v-model="form.matchIntroduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="赛事logo" prop="matchLogo">
          <image-upload v-model="form.matchLogo"/>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="比赛日期" prop="matchDate">
              <el-date-picker clearable
                              v-model="form.matchDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择比赛日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="16">
<!--            <el-form-item label="开始时间" prop="matchStartTime">-->
<!--              <el-input v-model="form.matchStartTime" placeholder="请输入开始时间" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="结束时间" prop="matchEndTime">-->
<!--              <el-input v-model="form.matchEndTime" placeholder="请输入结束时间" />-->
<!--            </el-form-item>-->
            <el-form-item label="开始时间" >
              <template>
                <el-time-select
                  v-model="form.matchStartTime"
                  :picker-options="{
                start: '08:00',
                step: '00:15',
                end: '18:00'
              }"
                  placeholder="请选择开始时间">
                </el-time-select>
                -
                <el-time-select
                  v-model="form.matchEndTime"
                  :picker-options="{
                start: '08:00',
                step: '00:15',
                end: '18:00'
              }"
                  placeholder="请选择结束时间">
                </el-time-select>
              </template>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="接待人数" prop="matchMaxPeople">
              <el-input-number
                v-model="form.matchMaxPeople"
                controls-position="right"
                :min="0"
                :max="1000000000"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预约人数" prop="matchAppointmentsPeople">
              <el-input-number
                v-model="form.matchAppointmentsPeople"
                controls-position="right"
                :min="0"
                :max="form.matchMaxPeople"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMatch, getMatch, delMatch, addMatch, updateMatch } from "@/api/match/match";
import { listVenue, getVenue, delVenue, addVenue, updateVenue } from "@/api/venue/venue";

export default {
  name: "Match",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 赛事管理表格数据
      matchList: [],
      // 场馆表格数据
      venueList: [],
      venueId:"",
      // 场地表格数据
      fieldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fieldId: null,
        matchName: null,
        matchIntroduce: null,
        matchLogo: null,
        matchDate: null,
        matchStartTime: null,
        matchEndTime: null,
        matchMaxPeople: null,
        matchAppointmentsPeople: null,

        fieldName: null,
        venueName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**
     * 获取场地
     */
    clickVenue(venueId){
      getVenue(venueId).then(response => {
        this.fieldList = response.data.fieldList;
        this.form.fieldId=null;
      });
    },
    /** 查询赛事管理列表 */
    getList() {
      this.loading = true;
      listMatch(this.queryParams).then(response => {
        this.matchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询场馆列表 */
    getVenueList() {
      listVenue().then(response => {
        this.venueList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        matchId: null,
        fieldId: null,
        matchName: null,
        matchIntroduce: null,
        matchLogo: null,
        matchDate: null,
        matchStartTime: null,
        matchEndTime: null,
        matchMaxPeople: null,
        matchAppointmentsPeople: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        fieldName: null,
        venueName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.matchId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加赛事管理";
      this.getVenueList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getVenueList();//获取场馆
      this.reset();
      const matchId = row.matchId || this.ids
      getMatch(matchId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改赛事管理";
        getVenue(response.data.venueId).then(response => {
          this.fieldList = response.data.fieldList;
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.matchId != null) {
            updateMatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const matchIds = row.matchId || this.ids;
      this.$modal.confirm('是否确认删除赛事管理编号为"' + matchIds + '"的数据项？').then(function() {
        return delMatch(matchIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('match/match/export', {
        ...this.queryParams
      }, `match_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
